
//
//  Map.swift
//  icar
//
//  Created by gongkai on 15/4/30.
//  Copyright (c) 2015年 anytracking. All rights reserved.
//

import Foundation
import MapKit

class Map: NSObject {
    
    //多个点中匹配范围
    class func regionWithLocations(coords:[CLLocationCoordinate2D])->MKCoordinateRegion?{
        if(coords.count==0){
            return nil
        }
        //center
        var max_long = coords[0].longitude
        var min_long = max_long
        var max_lat = coords[0].latitude
        var min_lat = max_lat
        for coord in coords{
            var latitude: CLLocationDegrees = coord.latitude
            var longitude: CLLocationDegrees = coord.longitude
            if (latitude < min_lat) { min_lat = latitude }
            if (longitude < min_long) {  min_long = longitude }
            if (latitude > max_lat) { max_lat = latitude }
            if (longitude > max_long) {  max_long = longitude }
        }
        let center_long = (max_long + min_long) * 0.5
        let center_lat = (max_lat + min_lat) * 0.5
        
        //span
        var abslat = abs(max_lat - min_lat)
        var abslon = abs(max_long - min_long)
        var deltalat = 0.005
        var deltalon = 0.005
        if (abslat > 0.5) {
            deltalat = 0.1
        } else if (abslat > 0.1) {
            deltalat = 0.05
        }
        
        if (abslon > 0.5) {
            deltalon = 0.1
        } else if (abslon > 0.1) {
            deltalon = 0.05
        }
        let span = MKCoordinateSpanMake(abslat+deltalat*2,  abslon+deltalon*2)
        return MKCoordinateRegion(center: CLLocationCoordinate2DMake(center_lat, center_long), span: span)
    }
    
}
